package linuxbox.admin.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

import linuxbox.BoxStatic;
import linuxbox.admin.logic.*;
import linuxbox.admin.bean.*;

/**
 * This class displays a user group member list. 
 */
public class GroupMemberList extends TagSupport {
    protected String listType = null;
    protected String memberType = null;
    protected String groupID = null;
    protected int selected = -1;
    
    public int doStartTag() throws JspException {
        try {
            UserManager um = UserManager.getInstance();
            Collection ids = null;
            boolean inOrOut = false;
            if ("in".equalsIgnoreCase(memberType)) {
                ids = um.getGrpMembers(Integer.parseInt(groupID));
                inOrOut = true;
            } else if ("out".equalsIgnoreCase(memberType)) {
                ids = um.getNotGrpMembers(Integer.parseInt(groupID));
                inOrOut = false;
            }
                        
            JspWriter out = pageContext.getOut();
            
            User user = null;
            UserCtrlKey key = null;
            
            for (Iterator it = ids.iterator(); it.hasNext(); ) {
                key = (UserCtrlKey)it.next();
                user = um.getUser(key);
                StringBuffer sb = new StringBuffer();

                if ("checkbox".equals(listType)) {
                    sb.append("<INPUT TYPE=\"checkbox\"");
                    if (inOrOut) {
                        sb.append(" NAME=\"member\"");
                    } else {
                        sb.append(" name=\"no_member\"");
                    }
                    sb.append(" VALUE=\"" + user.getID() + "\"> ");
                    sb.append(user.getFirstName() + " " + user.getLastName() + "<BR>");
                } else if ("select".equals(listType)) {
                    sb.append("<option value=\"" + user.getID() + "\">");
                    sb.append(user.getFirstName() + " " + user.getLastName());
                    sb.append("</option>");
                }
                out.println(sb.toString());
                out.newLine();
            }
                        
            out.flush();
        } catch(Exception e) {
            BoxStatic.logger.error("Failed to get all users");
            e.printStackTrace();
            throw new JspException(e.getMessage());
        }
        
        return SKIP_BODY;
    }

    public void setListType(String aValue) {
        listType = aValue;
    }

    public void setSelected(String aValue) {
        selected = Integer.parseInt(aValue);
    }

    public void setGroupID(String str) {
        groupID = str;
    }

    public void setMemberType(String str) {
        memberType = str;
    }
}
